<?php
namespace backend\modules\system\controllers;
use backend\controllers\BaseController;
use common\models\SysRole;
use common\models\SysMenu;
use yii\base\Object;
use common\models\SysRoleMenu;
use yii\helpers\Url;
/**
 * 权限管理
 * @author     chenxiaolin
 * @date        2016年3月15日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class RolemenuController extends BaseController{
    /**
     * 角色列表
     * @date: 2016年3月15日 
     * @author: chenxiaolin
     */
    public function actionRolelist(){
        $role = SysRole::find()->all();
        return $this->render('rolelist',['data' => $role]);
    }
    /**
     * 分配权限
     * @date: 2016年3月15日 
     * @author: chenxiaolin
     */
    public function actionAddaccess(){
        if(!$this->request->isPost){
            $id = $this->request->get('id',0);
            if($id < 1){
                $this->error("参数错误！");
            }
            //角色
            $role = SysRole::find()->where('id = :id',[':id' => $id])->one();
            if(!$role){
                $this->error("角色不存在！");
            }
            //父菜单列表
            $menuParent = SysMenu::find()->where('parent_id = 0')->all();
            //子菜单列表
            //$menuChild = SysMenu::find()->where('parent_id <> 0')->all();
            //当前角色有的权限
            $roleMenu = SysRoleMenu::find()->where('role_id = :id',[':id' => $id])->all();
            
            $menuIds = [];
            foreach ($roleMenu as $k => $v){
                $menuIds[$k] = $v['menu_id'];
            }
            
            return $this->render('addaccess',[
                'role' => $role,
                'menuParent' => $menuParent,
                //'menuChild' => $menuChild,
                'menuIds' => $menuIds
            ]);
        }
        //获取数据
        $menu = $this->request->post('menu');
        $role_id = $this->request->post('role_id');
        $menuStr = implode(',', $menu);
        //父菜单列表
        $parent = SysMenu::find()->where("id in ($menuStr)")->all();
        //子菜单列表
        //$child = SysMenu::find()->where("parent_id in ($menuStr)")->groupBy('controller_name')->all();
        $child = SysMenu::find()->where("parent_id in ($menuStr)")->all();
        $roleMenuIds = [];
        foreach ($parent as $pk => $pv){
            $roleMenuIds[$pk] = $pv['id'];
        }
        foreach ($child as $ck => $cv){
            array_push($roleMenuIds, $cv['id']);
        }
        $transAction = \Yii::$app->db->beginTransaction();
        try {
            //分配权限之前删除原有权限
            SysRoleMenu::deleteAll("role_id = $role_id");
            //保存数据
            for($i=0;$i<count($roleMenuIds);$i++){
                $menuObj = new SysRoleMenu();
                $menuObj->role_id = $role_id;
                $menuObj->menu_id = $roleMenuIds[$i];
                $menuObj->save(false);
            }
            $transAction->commit();
        } catch (\Exception $e) {
            $transAction->rollBack();
            $this->error('分配权限失败！');
        }
        $this->redirect(Url::toRoute(['rolemenu/addaccess','id' => $role_id]));
    }
}